package com.yingjie.yingjiefile.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.yingjie.commonbase.commons.Constents;
import com.yingjie.commonbase.commons.ReturnObject;
import com.yingjie.yingjiefile.config.OSSGenerateSignature;
import com.yingjie.yingjiefile.model.PublicFiles;
import com.yingjie.yingjiefile.service.PublicFilesService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/oss")
public class FileController {

    @Resource
    private PublicFilesService publicFilesService;
    //oss 直传获取签名
    @GetMapping("/generate_signature")
    public Object generate_signature() throws JsonProcessingException {
        OSSGenerateSignature OSSGenerateSignature = new OSSGenerateSignature();
        return ReturnObject.SUCCESS(OSSGenerateSignature.getSignature());
    }
    //oss上传文件后，录入到数据库
    @PostMapping("/insertFileInfo")
    public Object insertFileInfo(@RequestBody PublicFiles publicFiles )  {
        publicFiles.setCreatedAt(new Date());
        Integer result = publicFilesService.insertFileInfo(publicFiles);
        if(result==0){
            return ReturnObject.ERROR("添加文件失败");
        }
        Map map = new HashMap<>();
        map.put("fileId",publicFiles.getPublicFilesId());
        map.put("url", Constents.OSS_FILEPATH + publicFiles.getFileUrl());
        return ReturnObject.SUCCESS(map);
    }
}
